package com.huawei.esdk.tp.device.smc;

import java.util.List;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.device.smc.convert.TPSiteMgrConvert;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SiteFreeBusyRes;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySiteFreeBusyResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySiteFreeBusySouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SynchSiteFreeBusyResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SynchSiteFreeBusySouth;

public class SiteManagerCapabilityImpl extends V1R1C01BaseCapability implements
        ISiteManagerCapability
{

    public SiteManagerCapabilityImpl(
            ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        super(cxfSOAPProtocolAdapter);
    }

    private TPSiteMgrConvert convert = ApplicationContextUtil
            .getBean("tpSiteMgrConvert");

    /**
     * * 查询会场信息
     * @param query 领域bean
     * @return SDKResult 公用返回值
     */
    @Override
    public SDKResult<PagedList<Site>> querySites(QuerySites query)
    {
        QuerySitesSouth south = convert.querySites2QuerySitesSouth(query);
        QuerySitesResponseSouth southResponse = null;
        SDKResult<PagedList<Site>> result = new  SDKResult<PagedList<Site>>();
        try
        {
            southResponse = (QuerySitesResponseSouth) sendProtocolMessage(south,
                    "querySites");
            result = convert
                    .querySitesResponseSouth2SDKResult(southResponse);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }

        return result;
    }

    @Override
    public SiteFreeBusyRes querySiteFreeBusy(List<String> siteUris,
            DateTimeRange timeRange)
    {
        QuerySiteFreeBusySouth reqBean = convert
                .querySiteFreeBusy2QuerySiteFreeBusySouth(siteUris, timeRange);

        QuerySiteFreeBusyResponseSouth resBean = null;
        SiteFreeBusyRes siteFreeBusyRes = new  SiteFreeBusyRes();
        try
        {
            resBean = (QuerySiteFreeBusyResponseSouth) sendProtocolMessage(
                    reqBean, "querySiteFreeBusy");
            siteFreeBusyRes = convert.querySiteFreeBusyResponseSouth2SDKResult(resBean);
        }
        catch (ProtocolAdapterException e)
        {
            SDKErrorCode code = new SDKErrorCode();
            code.setErrCode(e.getErrorCode());
            siteFreeBusyRes.setResult(code);
        }

        return siteFreeBusyRes;
    }
    
    @Override
    public SiteFreeBusyRes synchSiteFreeBusy(List<String> siteUris,
            DateTimeRange timeRange)
    {
        SynchSiteFreeBusySouth reqBean = convert
                .synchSiteFreeBusy2SynchSiteFreeBusySouth(siteUris, timeRange);

        SynchSiteFreeBusyResponseSouth resBean = null;
        SiteFreeBusyRes siteFreeBusyRes = new SiteFreeBusyRes();
        try
        {
            resBean = (SynchSiteFreeBusyResponseSouth) sendProtocolMessage(
                    reqBean, "synchSiteFreeBusy");
            siteFreeBusyRes = convert
            .synchSiteFreeBusyResponseSouth2SDKResult(resBean); 
        }
        catch (ProtocolAdapterException e)
        {
            SDKErrorCode code = new SDKErrorCode();
            code.setErrCode(e.getErrorCode());
            siteFreeBusyRes.setResult(code);
        }

        return siteFreeBusyRes;
    }
}
